
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@page import="com.tss.util.*"%>
<%@page import="com.jedi.*"%>
<%@page import="com.store.*"%>
<%@page import="java.util.*"%>
<%@page import="java.io.File"%>
<%@page import="java.text.*"%>
<%@include file="../global_var.jsp"%>
<%@include file="../check_login.jsp"%>
<%
User just_user = (User)session.getAttribute("LoginUser");
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>
<html>
<head>
   <base href="<%=basePath%>">
<title>上传文档</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta http-equiv="pragma" content="no-cache">
<style type="text/css">
<!--
body {
	font-family: "宋体";
	font-size: 12px;
	line-height: 24px;
}
.moditext {
	font-family: "宋体", "黑体";
	font-size: 12px;
	line-height: 14px;
	color: #333333;
	margin: 0px;
	padding: 3px 0px 0px 1px;
	height: 20px;
	border: 1px solid #3E596A;
}
td {
	font-family: "宋体";
	font-size: 12px;
	line-height: 20px;
}
.mybtn {
	font-family: "宋体", "黑体";
	font-size: 12px;
	height: 20px;
	padding: 3px 0px 0px 2px;
	border: 1px solid #003330;
	background-color: #F3F3F3;
	color: #000000;
	cursor:hand;
}
-->
</style>
</head>

<%!
public String getExtName(String fileName) {
	int p = -1;
	while (fileName.indexOf(".",p) >= 0) {
		p = fileName.indexOf(".",p) + 1;
	}
	if (p > 0)
		return fileName.substring(p);
	else
		return "";
}
%>
<jsp:useBean id="smartUpload" scope="page" class="com.jspsmart.upload.SmartUpload"/>
<%
String savePath = "upload/knowledge/soft";
int maxSize = 1000; //以K为单位。
int minSize=10;
String fileType = ",jsp";

String uploadErr="";

File updir = new File(pageContext.getServletContext().getRealPath(savePath)); 
//File updir = new File(savePath);
if(!updir.exists()) updir.mkdir();

smartUpload.initialize(pageContext);
smartUpload.upload();

String retFileName = "";

for(int i=0;i < smartUpload.getFiles().getCount(); i++)
{
	com.jspsmart.upload.File fileObj = smartUpload.getFiles().getFile(i);
	if (!fileObj.isMissing()) 
	{
		int fileSize = fileObj.getSize();
		String fileName = fileObj.getFileName();
		String extName = getExtName(fileName);

		if (uploadErr.equals("") && (!extName.trim().equals("") && fileType.indexOf("," + extName) >= 0)) {
			uploadErr = "文件" + fileName + "的类型受限，不能上传！";
		}
		

			if (uploadErr.equals("") && fileSize < minSize*1024)
		{
			uploadErr = fileName + "文件太小，文件不能小于 " + minSize + "K！";
		}

		
		//fileName = KeyGen.nextID("");
		//if (!extName.trim().equals("")) fileName += "." + extName;
		String sFileName = savePath + "/" + fileName;
		
	
		//if (uploadErr.equals(""))
		//{
		//	File fileSave = new File(pageContext.getServletContext().getRealPath(savePath + "/" + fileName));
		//	if (fileSave.exists()) 
			//	uploadErr = "文件名已在存在，请更改要上传的文件！";
		//}
		

		if (uploadErr.equals(""))
		{
			retFileName = fileName;
			fileObj.saveAs(sFileName,smartUpload.SAVE_VIRTUAL);
		}
	}
	else
	{
		uploadErr="没有选择要上传的文件！";
	}
	
	if (!uploadErr.equals("")) break;
}
if (!uploadErr.equals("")) {
%>
<script language="javascript">
alert("<%=uploadErr%>");
location.href = "soft_upload.jsp";
</script>
<%
} else {
%>
<script language="javascript">
	parent.window.returnValue="<%=retFileName%>";
	parent.window.close();
</script>
<%}%>